package com.cyys.common.thirdInterface.aliyun;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import com.cyys.common.thirdInterface.ThirdConfig;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * OSS 工具类
 */
public class OssUtil {

    /**
     *OSS上传文件
     * @param inputStream 文件流
     * @param fileName 文件名
     * @return 返回上传结果
     * @
     */
    public static Boolean upLoadFile(InputStream inputStream,String fileName) {
        //打開OSS
        OSSClient  ossClient = new OSSClient(ThirdConfig.ALY_OSS_ENDPOINT, ThirdConfig.ALY_OSS_ACCESS_KEYID, ThirdConfig.ALY_OSS_ACCESS_KEYSECRET);
        //存入图片
        PutObjectResult result=ossClient.putObject(ThirdConfig.ALY_OSS_CONVENIENCE_BUCKETNAME,fileName,inputStream);
        //关闭OSS
        ossClient.shutdown();
        return true;

    }

    /**
     * OSS刪除單個文件
     * @param fileName 文件名
     * @
     */
    public static void delFile(String fileName) {
        //打開OSS
        OSSClient  ossClient = new OSSClient(ThirdConfig.ALY_OSS_ENDPOINT, ThirdConfig.ALY_OSS_ACCESS_KEYID, ThirdConfig.ALY_OSS_ACCESS_KEYSECRET);
        // 删除文件。
        ossClient.deleteObject(ThirdConfig.ALY_OSS_CONVENIENCE_BUCKETNAME, fileName);
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     * OSS 刪除多文件
     * @param fileNameList 多文件list集合
     * @
     */
    public static void delFiles(List<String> fileNameList) {
        //打開OSS
        OSSClient  ossClient = new OSSClient(ThirdConfig.ALY_OSS_ENDPOINT, ThirdConfig.ALY_OSS_ACCESS_KEYID, ThirdConfig.ALY_OSS_ACCESS_KEYSECRET);
        //刪除多文件
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(ThirdConfig.ALY_OSS_CONVENIENCE_BUCKETNAME).withKeys(fileNameList));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     *  OSS查询文件
     * @param floaderName  文件夹名称
     * @return
     */
    public static List<String> listFolder(String floaderName) {
        List<String> files=new ArrayList<>();
        OSSClient  ossClient = new OSSClient(ThirdConfig.ALY_OSS_ENDPOINT, ThirdConfig.ALY_OSS_ACCESS_KEYID, ThirdConfig.ALY_OSS_ACCESS_KEYSECRET);
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(ThirdConfig.ALY_OSS_CONVENIENCE_BUCKETNAME);
        listObjectsRequest.setDelimiter("/");
        listObjectsRequest.setPrefix(floaderName);

        ObjectListing listing = ossClient.listObjects(listObjectsRequest);
        for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
            System.out.println(objectSummary.getKey());
            files.add(objectSummary.getKey());
        }
        for (String commonPrefix : listing.getCommonPrefixes()) {
            files.add(commonPrefix);
        }
        ossClient.shutdown();
        return  files;
    }

    /**
     * 查询文件是否存在
     * @param fileName 文件名
     * @return
     */
    public static Boolean isExist(String fileName) {
        boolean exist=false;
        OSSClient  ossClient = new OSSClient(ThirdConfig.ALY_OSS_ENDPOINT, ThirdConfig.ALY_OSS_ACCESS_KEYID, ThirdConfig.ALY_OSS_ACCESS_KEYSECRET);
        exist = ossClient.doesObjectExist(ThirdConfig.ALY_OSS_CONVENIENCE_BUCKETNAME, fileName);
        return  exist;
    }

}
